/*
 * Created: Author: Aléx Carvalho / Date: Mar 10, 2015
 * 
 * Changed: $Author: ac280593@gmail.com $ / $Date: 2015-06-11 04:31:49 +0000 (Thu, 11 Jun 2015) $
 */
package com.cpm.view.zk.pane;

import com.cpm.ServiceLocator;
import com.cpm.aggregate.SessionAttribute;
import com.cpm.entities.User;
import com.cpm.view.Element;
import com.cpm.view.zk.buttons.Button;
import com.cpm.view.zk.field.LabelField;
import com.cpm.view.zk.field.PasswordField;
import com.cpm.view.zk.field.TextField;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.HtmlBasedComponent;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Div;
import org.zkoss.zul.Hlayout;

public class LoginPane implements Element<HtmlBasedComponent> {

    private final Div div = new Div();

    public LoginPane() {

        TextField nametf = new TextField();
        nametf.setLabel("Nome");

        PasswordField passwordField = new PasswordField();
        passwordField.setLabel("Senha");

        Button button = new Button();
        button.setText("Entrar");

        Hlayout hlayoutName = new Hlayout();
        hlayoutName.appendChild(new LabelField(nametf.getLabel()).getElement());
        hlayoutName.appendChild(nametf.getElement());
        
        Hlayout hlayoutPass = new Hlayout();
        hlayoutPass.appendChild(new LabelField(passwordField.getLabel()).getElement());
        hlayoutPass.appendChild(passwordField.getElement());
        
        div.appendChild(hlayoutName);
        div.appendChild(hlayoutPass);
        div.appendChild(button.getElement());

        button.addEventListener(Events.ON_CLICK, e -> {
            ServiceLocator.getSessionContext().putAttribute(SessionAttribute.ACTIVE_USER, new User());
            
            Executions.sendRedirect(null);
        });
    }

    @Override
    public HtmlBasedComponent getElement() {
        return div;
    }
}
